// @Author EthanScriptOn
// @Desc
package config_processor_management

import (
	"errors"
	"gitee.com/fatzeng/srf_switch_basic_components/identity"
	"gitee.com/fatzeng/srf_switch_basic_components/initialization"
	"gitee.com/fatzeng/srf_switch_config/config_define"
	"math"
	"time"
)

var processorManagerIdentity = func() *initialization.OnceInitializer {
	initializer := initialization.GenerateOnceInitializer(config_define.ParserPositionTag, config_define.ProcessorManagerIdentityInitTag)
	err := initializer.Set(math.MaxInt, func() (result interface{}, err error) {
		snowflake, err := identity.GenerateSnowflake(&identity.SnowflakeConfig{Epoch: time.Now(), SequenceBits: 12})
		if err != nil {
			return nil, err
		}
		return identity.GeneratePrefixedObjectId(snowflake, "ProcessorManager"), nil
	})
	if err != nil {
		panic(err)
	}
	return initializer
}()

func GetProcessorManagerIdentity() (identity.IdentityInformation, error) {
	identityInstance, err := processorManagerIdentity.Get()
	if err != nil {
		return nil, err
	}
	information, ok := identityInstance.(identity.IdentityInformation)
	if !ok {
		return nil, errors.New("ProcessorManagerIdentity is not IdentityInformation")
	}
	return information, nil
}
